﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FzWeb.Models
{
    public class SearchService
    {
        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"] = new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }

        public IList<Post> PostSearch(PostSearch ps)
        {
            #region
            ////判断赋默认值确保传入后台数据库
            //if (string.IsNullOrEmpty(ps.VocationId)) ps.VocationId = "0";
            //if (string.IsNullOrEmpty(ps.PostClassId)) ps.PostClassId="0";
            //if (string.IsNullOrEmpty(ps.LocationId)) ps.LocationId = "0";
            //if (ps.PostDate == 0) ps.PostDate = 0;
            //if (ps.WorkLength == 0) ps.WorkLength = 0;
            //if (string.IsNullOrEmpty(ps.Education)) ps.Education = "0";
            //if (string.IsNullOrEmpty(ps.keyWord)) ps.keyWord = "0";
            //if (ps.keyWordType == 0) ps.keyWordType = 0;
            //if ((ps.PostType == "'','',''") || (string.IsNullOrEmpty(ps.PostType))) ps.PostType = "0";
            //if (string.IsNullOrEmpty(ps.CompanyType)) ps.CompanyType = "0";
            //if (ps.Sex == 0) ps.Sex = 0;
            //if (ps.SalaryFrom == 0) ps.SalaryFrom = 0;
            //if (ps.SalaryTo == 0) ps.SalaryTo = 0;

            //return db.pGetPost(ps.VocationId,
            //                   ps.PostClassId,
            //                   ps.LocationId,
            //                   ps.PostDate,
            //                   ps.WorkLength,
            //                   ps.Education,
            //                   ps.keyWord,
            //                   ps.keyWordType,
            //                   ps.PostType,
            //                   ps.CompanyType,
            //                   ps.Sex,
            //                   ps.SalaryFrom,
            //                   ps.SalaryTo
            //                   ).ToList();
            //ps.VocationId = ((ps.VocationId= null) ? '0' : ps.VocationId);
            //return db.pGetPost().toList();
            #endregion

            List<v_PostSearchList> list = db.v_PostSearchList.OrderByDescending(c => c.PostTime).ToList();
            if (!string.IsNullOrEmpty(ps.CompanyType))
                list = list.Where(c => c.ComType == ps.CompanyType).ToList();
            if (ps.PostDate != 0)
            {
                DateTime dt = DateTime.Now;
                DateTime dt2 = dt.AddDays(-ps.PostDate);
                list = list.Where(c => c.PostTime >= dt2).ToList();
            }
            if (ps.WorkLength != 0)
                list = list.Where(c => !string.IsNullOrEmpty(c.WorkYear)).Where(c => Int32.Parse(c.WorkYear) >= ps.WorkLength).ToList();

            string str = "'" + ((ps.PostType.Split(',')[0] == "'true'") ? "全职" : "") + "'" + "," +
                         "'" + ((ps.PostType.Split(',')[1] == "'true'") ? "兼职" : "") + "'" + "," +
                         "'" + ((ps.PostType.Split(',')[2] == "'true'") ? "实习" : "") + "'";
            if ((str != "'','',''") && (!string.IsNullOrEmpty(str)))
            {
                list = list.Where(c => str.IndexOf(c.Type) >= 0).ToList();
            }
            if (!string.IsNullOrEmpty(ps.CompanyType))
                list = list.Where(c => c.ComType == ps.CompanyType).ToList();
            if (!string.IsNullOrEmpty(ps.Education))
                list = list.Where(c => c.Education == ps.Education).ToList();
            if (ps.Sex != 0)
                list = list.Where(c => c.Sex == ps.Sex).ToList();
            if (ps.SalaryFrom != 0)
                list = list.Where(c => c.Salary >= ps.SalaryFrom).ToList();
            if (ps.SalaryTo != 0)
                list = list.Where(c => c.Salary <= ps.SalaryTo).ToList();
            //2公司名称搜索，1职位名称搜索,全文暂时没处理
            if ((!string.IsNullOrEmpty(ps.keyWord)) && (ps.keyWord != "输入岗位关键字"))
            {
                if (ps.keyWordType == 2)
                    list = list.Where(c => c.ComName.IndexOf(ps.keyWord) >= 0).ToList();
                if (ps.keyWordType == 1)
                    list = list.Where(c => c.Name.IndexOf(ps.keyWord) >= 0).ToList();
            }

            if (!string.IsNullOrEmpty(ps.VocationId))
            {
                list = list.Where(c => c.ComVocationId != null).ToList();
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PostSearchList vps = list[j];
                    int ncount = 0;
                    string[] strvaid = ps.VocationId.Split(',');
                    for (int k = 0; k <= strvaid.Length - 1; k++)
                    {
                        if (strvaid[k] == vps.ComVocationId.ToString())
                            ncount++;
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            if (!string.IsNullOrEmpty(ps.PostClassId))
            {
                //将POSTCLASS 分到最小粒度
                string[] currpostid = ps.PostClassId.Split(',');
                for (int i = 0; i <= currpostid.Length - 1; i++)
                {
                    if (Int32.Parse(currpostid[i]) > 10000)
                    {
                        int psid = Int32.Parse(currpostid[i].Substring(0, currpostid[i].Length - 5));
                        List<Post_Class> pclist = db.Post_Class.Where(c => c.Post_Parent.PostClassId == psid).ToList();
                        foreach (Post_Class pc in pclist)
                        {
                            ps.PostClassId = ps.PostClassId + ',' + pc.PostClassId;
                        }
                    }
                }
                //list = list.Where(c => c.PostClassId != null).Where(c => ps.PostClassId.IndexOf(c.PostClassId.ToString()) >= 0).ToList();
                list = list.Where(c => c.PostClassId != null).ToList();
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PostSearchList vps = list[j];
                    int ncount = 0;
                    string[] strpsid = ps.PostClassId.Split(',');
                    for (int k = 0; k <= strpsid.Length - 1; k++)
                    {
                        if (strpsid[k] == vps.PostClassId.ToString())
                            ncount++;
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            //取地区匹配
            if (!string.IsNullOrEmpty(ps.LocationId))
            {
                string[] currlocaid = ps.LocationId.Split(',');
                for (int i = 0; i <= currlocaid.Length - 1; i++)
                {
                    if (Int32.Parse(currlocaid[i]) > 10000)
                    {
                        int locaid = Int32.Parse(currlocaid[i].Substring(0, currlocaid[i].Length - 5));
                        List<Area> arealist = db.Area.Where(c => c.Parent.AreaId == locaid).ToList();
                        foreach (Area area in arealist)
                        {
                            ps.LocationId = ps.LocationId + ',' + area.AreaId;
                        }
                    }
                }
                //循环处理记录
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PostSearchList vps = list[j];
                    string[] cur_locaid = vps.LocationId.Split(',');
                    for (int i = 0; i <= cur_locaid.Length - 1; i++)
                    {
                        if (Int32.Parse(cur_locaid[i]) > 10000)
                        {
                            int locaid = Int32.Parse(cur_locaid[i].Substring(0, cur_locaid[i].Length - 5));
                            List<Area> arealist = db.Area.Where(c => c.Parent.AreaId == locaid).ToList();
                            foreach (Area area in arealist)
                            {
                                vps.LocationId = vps.LocationId + ',' + area.AreaId;
                            }
                        }
                    }
                    string[] strlocaid = vps.LocationId.Split(',');
                    int ncount = 0;
                    for (int i = 0; i <= strlocaid.Length - 1; i++)
                    {
                        //if (ps.LocationId.IndexOf(strlocaid[i]) >= 0)
                        //    ncount++;
                        string[] strloid = ps.LocationId.Split(',');
                        for (int k = 0; k <= strloid.Length - 1; k++)
                        {
                            if (strloid[k] == strlocaid[i])
                                ncount++;
                        }
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            List<Post> pslist = new List<Post>();//=list
            Post pst;
            foreach (v_PostSearchList vp in list)
            {
                pst = new Post();
                //pst=db.Post.Include("company").First(c => c.PostId == vp.PostId);
                pst = db.Post.First(c => c.PostId == vp.PostId);
                pslist.Add(pst);
            }
            return pslist;
        }

        public IList<Person_Resume> PersonSearch(PersonSearch ps)
        {
            List<v_PersonSearchList> list = db.v_PersonSearchList.ToList();

            string str = "'" + ((ps.PostType.Split(',')[0] == "'true'") ? "全职" : "") + "'" + "," +
                         "'" + ((ps.PostType.Split(',')[1] == "'true'") ? "兼职" : "") + "'" + "," +
                         "'" + ((ps.PostType.Split(',')[2] == "'true'") ? "实习" : "") + "'";
            if ((str != "'','',''") && (!string.IsNullOrEmpty(str)))
            {
                list = list.Where(c => c.JobType != null && c.JobType != "").ToList();
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PersonSearchList vps = list[j];
                    string[] cur_posttype = vps.JobType.Split(',');
                    if (cur_posttype[0] == "true")
                        vps.JobType = vps.JobType + ',' + "全职";
                    if (cur_posttype[1] == "true")
                        vps.JobType = vps.JobType + ',' + "兼职";
                    if (cur_posttype[2] == "true")
                        vps.JobType = vps.JobType + ',' + "实习";
                    string[] posttypes = vps.JobType.Split(',');
                    int ncount = 0;
                    for (int i = 0; i <= posttypes.Length - 1; i++)
                    {
                        if (str.IndexOf(posttypes[i]) >= 0)
                            ncount++;
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }
            if (ps.Agefrom != 0)
                list = list.Where(c => (DateTime.Now.Year - c.Birthday.Value.Year) + 1 >= ps.Agefrom).ToList();
            if (ps.Ageto != 0)
                list = list.Where(c => (DateTime.Now.Year - c.Birthday.Value.Year) + 1 <= ps.Ageto).ToList();
            if (!string.IsNullOrEmpty(ps.Education))
                list = list.Where(c => c.Education == ps.Education).ToList();
            if (ps.Ispic)
                list = list.Where(c => c.Photo != null).ToList();
            if (!string.IsNullOrEmpty(ps.Keyword))
            {
                // "1"-"全文";"2"-"职位名称";"3"-"公司名称");
                //ps.Keywordtype
                //if (ps.keyWordType == 2)
                //    list = list.Where(c => c.ComName.IndexOf(ps.Keyword) >= 0).ToList();
                //if (ps.keyWordType == 1)
                //    list = list.Where(c => c.Name.IndexOf(ps.Keyword) >= 0).ToList();
            }
            if (ps.Lastupdate != 0)
            {
                DateTime dt = DateTime.Now;
                DateTime dt2 = dt.AddDays(-ps.Lastupdate);
                list = list.Where(c => c.RefleshTime >= dt2).ToList();
            }
            if (!string.IsNullOrEmpty(ps.Residenceid))
                list = list.Where(c => c.Residence == ps.Residenceid).ToList();
            if (ps.Sex == 1)
                list = list.Where(c => c.Sex == true).ToList();
            if (ps.Sex == 2)
                list = list.Where(c => c.Sex == false).ToList();
            if (ps.Salary1 != 0)
                list = list.Where(c => c.SalaryMin >= ps.Salary1).ToList();
            if (ps.Salary2 != 0)
                list = list.Where(c => c.SalaryMax <= ps.Salary2).ToList();
            if (ps.Worklength != 0)
            {
                //表中字段为空的数据移除出LIST
                list = list.Where(c => c.WorkLength != null && c.WorkLength != "").ToList();
                list = list.Where(c => Int32.Parse(c.WorkLength) >= ps.Worklength).ToList();
            }
            if (!string.IsNullOrEmpty(ps.LocationId))
            {
                list = list.Where(c => c.LocationId != null&&c.LocationId!="").ToList();
                string[] currlocaid = ps.LocationId.Split(',');
                for (int i = 0; i <= currlocaid.Length - 1; i++)
                {
                    if (Int32.Parse(currlocaid[i]) > 10000)
                    {
                        int locaid = Int32.Parse(currlocaid[i].Substring(0, currlocaid[i].Length - 5));
                        List<Area> arealist = db.Area.Where(c => c.Parent.AreaId == locaid).ToList();
                        foreach (Area area in arealist)
                        {
                            ps.LocationId = ps.LocationId + ',' + area.AreaId;
                        }
                    }
                }
                //循环处理记录
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PersonSearchList vps = list[j];
                    string[] cur_locaid = vps.LocationId.Split(',');
                    for (int i = 0; i <= cur_locaid.Length - 1; i++)
                    {
                        if (Int32.Parse(cur_locaid[i]) > 10000)
                        {
                            int locaid = Int32.Parse(cur_locaid[i].Substring(0, cur_locaid[i].Length - 5));
                            List<Area> arealist = db.Area.Where(c => c.Parent.AreaId == locaid).ToList();
                            foreach (Area area in arealist)
                            {
                                vps.LocationId = vps.LocationId + ',' + area.AreaId;
                            }
                        }
                    }
                    string[] strlocaid = vps.LocationId.Split(',');
                    int ncount = 0;
                    for (int i = 0; i <= strlocaid.Length - 1; i++)
                    {
                        //if (ps.LocationId.IndexOf(strlocaid[i]) >= 0)
                        //    ncount++;
                        string[] strloid = ps.LocationId.Split(',');
                        for (int k = 0; k <= strloid.Length - 1; k++)
                        {
                            if (strloid[k] == strlocaid[i])
                                ncount++;
                        }
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            if (!string.IsNullOrEmpty(ps.PostClassId))
            {
                list = list.Where(c => c.PostClassId != null&&c.PostClassId!="").ToList();
                string[] currpsid = ps.PostClassId.Split(',');
                for (int i = 0; i <= currpsid.Length - 1; i++)
                {
                    if (Int32.Parse(currpsid[i]) > 10000)
                    {
                        int pcid = Int32.Parse(currpsid[i].Substring(0, currpsid[i].Length - 5));
                        List<Post_Class> pclist = db.Post_Class.Where(c => c.Post_Parent.PostClassId == pcid).ToList();
                        foreach (Post_Class postclass in pclist)
                        {
                            ps.PostClassId = ps.PostClassId + ',' + postclass.PostClassId;
                        }
                    }
                }
                ////移除POSTCLSSID中大的字串，避免字串做查询时的错误匹配
                //string[] pclists = ps.Postclassid.Split(',');
                //string strpcid="";
                //for (int j = 0; j < pclists.Length - 1; j++)
                //{
                //    if (Int32.Parse(pclists[j]) <= 10000) 
                //    {
                //        strpcid = strpcid + ',' + pclists[j];
                //    } 
                //}
                //ps.Postclassid = strpcid;

                //循环处理记录 不符合条件的对象移除
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PersonSearchList vps = list[j];
                    string[] cur_pcid = vps.PostClassId.Split(',');
                    for (int i = 0; i <= cur_pcid.Length - 1; i++)
                    {
                        if (Int32.Parse(cur_pcid[i]) > 10000)
                        {
                            int pcid = Int32.Parse(cur_pcid[i].Substring(0, cur_pcid[i].Length - 5));
                            List<Post_Class> pclist = db.Post_Class.Where(c => c.Post_Parent.PostClassId == pcid).ToList();
                            foreach (Post_Class postclass in pclist)
                            {
                                vps.PostClassId = vps.PostClassId + ',' + postclass.PostClassId;
                            }
                        }
                    }
                    string[] strlocaid = vps.PostClassId.Split(',');
                    int ncount = 0;
                    for (int i = 0; i <= strlocaid.Length - 1; i++)
                    {
                        //if (ps.Postclassid.IndexOf(strlocaid[i]) >= 0)
                        string[] strpclid = ps.PostClassId.Split(',');
                        for (int k = 0; k <= strpclid.Length - 1; k++)
                        {
                            if (strpclid[k] == strlocaid[i])
                                ncount++;
                        }
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            if (!string.IsNullOrEmpty(ps.VocationId))
            {
                //增加判断下LIST集合避免期望行业为空的数据进入集合
                list = list.Where(c => c.VocationId != null&&c.VocationId!="").ToList();
                for (int j = 0; j <= list.Count - 1; j++)
                {
                    v_PersonSearchList vps = list[j];
                    string[] strlocaid = vps.VocationId.Split(',');
                    int ncount = 0;
                    for (int i = 0; i <= strlocaid.Length - 1; i++)
                    {
                        //if (ps.Vocationid.IndexOf(strlocaid[i]) >= 0)
                        string[] strvoid = ps.VocationId.Split(',');
                        for (int k = 0; k <= strvoid.Length - 1; k++)
                        {
                            if (strvoid[k] == strlocaid[i])
                                ncount++;
                        }
                    }
                    if (ncount == 0)
                    {
                        list.Remove(vps);
                        j--;
                    }
                }
            }

            List<Person_Resume> pslist = new List<Person_Resume>();//=list
            Person_Resume pr;
            foreach (v_PersonSearchList vp in list)
            {
                pr = new Person_Resume();
                //pr = db.Person_Resume.Include("Person_Work_Experience").
                //Include("person").Include("person.user").
                //Include("Person_Train").Include("Person_Eduction").
                //Include("Person_Skill").Include("Person_WillJob").First(c => c.ResumeId == vp.ResumeId);
                pr = db.Person_Resume.First(c => c.ResumeId == vp.ResumeId);
                pslist.Add(pr);
            }
            return pslist;
        }

        public IList<Post> ComPostSearch(ComPostSearch cps)
        {
            List<Post> list = db.Post.Where(c => c.Company.CompanyId == cps.Comid).ToList();
            if (cps.PostState != null)
                list = list.Where(c => c.Status == cps.PostState).ToList();
            if (cps.SRefreshDate != null)
                list = list.Where(c => c.PostTime >= cps.SRefreshDate).ToList();
            if (cps.TRefreshDate != null)
                list = list.Where(c => c.PostTime <= cps.TRefreshDate).ToList();
            if (cps.StartDate != null)
                list = list.Where(c => c.EndDate >= cps.StartDate).ToList();
            if (cps.EndDate != null)
                list = list.Where(c => c.EndDate <= cps.EndDate).ToList();
            return list;
        }
    }
}
